import {doGet} from "@/config/http";
import {ref, toRefs} from "vue";
import useDictStore from "@/store/modules/dict";
import {svc} from "@/config/common";

export function useDict(...args) {
    const res = ref({});
    return (() => {
        args.forEach((dictType, index) => {
            console.log('index', index)
            res.value[dictType] = [];
            const dicts = useDictStore().getDicts(dictType);
            if (dicts) {
                res.value[dictType] = dicts;
            } else {
                doGet(svc, '/dict-value/listByField', {field: 'dictType', values: dictType}, false).then(resp => {
                    res.value[dictType] = resp?.map(item => ({
                        label: item.value,
                        value: item.key,
                        elTagType: item['listClass'] || 'default',
                        elTagClass: item.state === '1' ? item['cssClass'] : "text-muted",
                        elTagStyle: item.elTagStyle,
                    }))
                    useDictStore().setDicts(dictType, res.value[dictType]);
                })
            }
        })
        return toRefs(res.value);
    })()
}
